// pages/coupon/detail.js
import api from '../../api/index'
import { AppId } from '../../api/config'
import { userInfoStore } from '../../store/userInfo'
var WXBizDataCrypt = require('../../utils/RdWXBizDataCrypt.js');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userLevel: 0,
    coupon:null
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ 
      userLevel: userInfoStore.userInfo.level
    })
    api.getcoupondetail({ couponId: options.couponId}).then(res=>{
      let { couponType} = res.result;
      this.setData({ 
        coupon:res.result
        });
    })
  },

  onShow: function () {
    
  },
  
  buy:function(){
    if (this.data.coupon){
      if(this.data.coupon.status!=1){
        wx.showToast({
          icon: "none",
          title: "敬请期待!"
        })
        return;
      }
      let today = new Date();
      let item = this.data.coupon;
      if (item.beginGrandTime) {
        let begin = new Date(item.beginGrandTime);
        if (today - begin < 0) {
          wx.showToast({
            icon: "none",
            title: "敬请期待!"
          })
          return;
        }
      }
      if (item.endGrandTime) {
        let end = new Date(item.endGrandTime);
        if (today - end > 0) {
          wx.showToast({
            icon: "none",
            title: "活动已结束,更多精彩敬请期待!"
          })
          return;
        }
      }


      wx.navigateTo({
        url: 'buy?couponId=' + this.data.coupon.id,
      })
      
    }
  }
  
})